<?php
class ServiceOrderRowsController extends AppController {  
	public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');
    public $uses = array('ServiceOrderRow', 'ServiceOrderRowRepaire', 'ServiceOrderRowState', 'State','CubiertaMarca','Client','Article', 'Repaire');


    public function pdf($id = null) {
        if (!$id) {
            $this->Session->setFlash('Sorry, there was no PDF selected.');
            $this->redirect(array('action'=>'index'), null, true);
        }
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
        $this->set('service_order', $this->ServiceOrderRow->read(null, $id));
        $this->set('states', $this->State->find('list', array('fields' => array('id', 'name'))));
        $this->set('service_order_rows', $this->ServiceOrderRowState->
            find('all', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id))));
        //$this->set('cubiertamarcas', $this->CubiertaMarca->find('list', array('fields' => array('id', 'nombre'))));
        //$this->set('service_order', $this->ServiceOrderRow->find('all',array('conditions' => array('ServiceOrderRow.service_order_id' => $id))));

        

        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        //$this->set('contenido', $this->generarTablaHTML());
        $this->render();
 
 }
        
    public function index($estado = null) {
        $userGroupPermission = $this->Session->read('Auth.User.Group.state_id');


        if ($userGroupPermission == 0) {
            //es un admin, checkea si requiere un estado en particular o todas las cubiertas.
            If ((isset($estado) && ($estado != null) && ($estado > 0))){
            $this->set('estado',$estado);
        
            } else{
                $this->set('estado',null);
            }
        } else{
            //es otro usuario y le mostramos solo las cubiertas de su sector.
            $this->set('estado',$userGroupPermission);    
        }
    	    	
    }
    
    public function view($id) {
    	//$this->ServiceOrder->id = $id;
    	$this->set('service_order', $this->ServiceOrderRow->read(null, $id));
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
        $this->set('states', $this->State->find('list', array('fields' => array('id', 'name'))));
    	$this->set('service_order_rows_repaire', $this->ServiceOrderRowRepaire->find('all', array('conditions' => array('ServiceOrderRowRepaire.service_order_row_id' => $id))));    
        $this->set('service_order_rows', $this->ServiceOrderRowState->find('all', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id))));    
    }
    
    public function createOrderRowDefaultState($id){
    	$newdata['service_order_row_id'] = $id;
    	// default initial state.
    	$firstState = $this->State->find('first', null);
    	$newdata['state_id'] =  $firstState['State']['id'];
    	$newdata['fecha'] =  date('Y-m-d h:i:s');
    	$this->ServiceOrderRowState->save($newdata);
    }
    
    public function add($order_id) {    	
        $this->set('cubiertamarcas', $this->CubiertaMarca->find('list', array('fields' => array('id', 'nombre'))));
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
    	$this->set('order_id', $order_id);
    	$this->set('firstState', $this->State->find('first', null));
    	if ($this->request->is('post')) {   		
    		$this->ServiceOrderRow->create();
    		if ($this->ServiceOrderRow->save($this->request->data)) {
    			$this->createOrderRowDefaultState($this->ServiceOrderRow->id);
    			$this->Session->setFlash('Se han guardado los cambios.');
    			$this->redirect(array('controller' => 'ServiceOrders','action' => 'view', $order_id ));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    public function edit($id = null) {
    	
    	$this->ServiceOrderRow->id = $id;
    	if ($this->request->is('get')) {
    		$this->request->data = $this->ServiceOrderRow->read();
    	} else {
    		if ($this->ServiceOrderRow->save($this->request->data)) {
    			$this->Session->setFlash('Datos actualizados correctamente.');
    			$this->redirect(array('action' => 'view'));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    public function inspeccion_inicial($id = null) {
    	//Agregado para reparaciones
        $this->set('service_order', $this->ServiceOrderRow->read(null, $id));
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
        $this->set('states', $this->State->find('list', array('fields' => array('id', 'name'))));
        $this->set('service_order_rows_repaire', $this->ServiceOrderRowRepaire->find('all', array('conditions' => array('ServiceOrderRowRepaire.service_order_row_id' => $id))));    
        $this->set('service_order_rows', $this->ServiceOrderRowState->find('all', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id))));    
        // Fin agregado para reparaciones
    	$this->ServiceOrderRow->id = $id;
    	if ($this->request->is('get')) {
    		$this->request->data = $this->ServiceOrderRow->read();

    	} else {
    		if ($this->ServiceOrderRow->save($this->request->data)) {
    			$this->Session->setFlash('Datos actualizados correctamente.');
                    ////////////////////////BEFORE///////////////////////////
                    //$sql = "UPDATE qilin.service_order_rows SET estado_cod = '2' WHERE service_order_rows.id =47;";
                    //$this->ServiceOrderRowState->query($sql);    
                    ////////////////////////BEFORE///////////////////////////

    			$this->redirect(array('action' => 'index'));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    public function inspeccion_final($id = null) {
        //Agregado para reparaciones
        $this->set('service_order', $this->ServiceOrderRow->read(null, $id));
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
        $this->set('states', $this->State->find('list', array('fields' => array('id', 'name'))));
        $this->set('service_order_rows_repaire', $this->ServiceOrderRowRepaire->find('all', array('conditions' => array('ServiceOrderRowRepaire.service_order_row_id' => $id))));    
        $this->set('service_order_rows', $this->ServiceOrderRowState->find('all', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id))));    
        // Fin agregado para reparaciones
        $this->ServiceOrderRow->id = $id;
        if ($this->request->is('get')) {
            $this->request->data = $this->ServiceOrderRow->read();

        } else {
            if ($this->ServiceOrderRow->save($this->request->data)) {
                $this->Session->setFlash('Datos actualizados correctamente.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('No se pudieron guardar los cambios.');
            }
        }
    }
    
	
	public function pulido($id = null) {
         
        $this->ServiceOrderRow->id = $id;
        
        if ($this->request->is('get')) {
            $this->request->data = $this->ServiceOrderRow->read();
        } else {
            if ($this->ServiceOrderRow->save($this->request->data)) {
                $this->Session->setFlash('Datos actualizados correctamente.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('No se pudieron guardar los cambios.');
            }
        }
    }
    
    public function embandado($id = null) {
        

        //Agregado para reparaciones
        $this->set('service_order', $this->ServiceOrderRow->read(null, $id));
        $this->set('articles', $this->Article->find('list', array('fields' => array('id', 'descripcion'))));
        $this->set('states', $this->State->find('list', array('fields' => array('id', 'name'))));
        $this->set('service_order_rows_repaire', $this->ServiceOrderRowRepaire->find('all', array('conditions' => array('ServiceOrderRowRepaire.service_order_row_id' => $id))));    
        $this->set('service_order_rows', $this->ServiceOrderRowState->find('all', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id))));    
        //Fin agregado para reparaciones


    	$this->ServiceOrderRow->id = $id;
    	if ($this->request->is('get')) {
            $this->request->data = $this->ServiceOrderRow->read();
    	      // echo "<pre>";
           //    print_r($this->request->data);
           //    echo "<pre>"; 

        } else {
    		if ($this->ServiceOrderRow->save($this->request->data)) {
                $this->Session->setFlash('Datos actualizados correctamente.');
    			$this->redirect(array('action' => 'index'));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    public function cocinado($id = null) {
    
    	$this->ServiceOrderRow->id = $id;
    	if ($this->request->is('get')) {
    		$this->request->data = $this->ServiceOrderRow->read();
    	} else {
    		if ($this->ServiceOrderRow->save($this->request->data)) {
    			$this->Session->setFlash('Datos actualizados correctamente.');
    			$this->redirect(array('action' => 'index'));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    
    public function beforeFilter() {
    	/*parent::beforeFilter();
    	$this->Auth->allow('index', 'view');*/
    }

    }